package com.yanqu.road.server.manager.kuang;

import com.yanqu.road.entity.kuang.group.KuangMatchGroup;
import com.yanqu.road.logic.bussiness.kuang.KuangBusiness;
import com.yanqu.road.logic.config.GameConfig;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.kuang.activity.CrossKuangActivity;
import com.yanqu.road.server.manger.ChannelSystemConfigMgr;
import com.yanqu.road.server.manger.ConfigMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 矿场争夺战
 */
public class CrossKuangMgr extends TempCrossMgr {

    /**
     * 分组信息
     */
    public final static Map<Integer, Map<Integer, KuangMatchGroup>> matchGroupMap = new ConcurrentHashMap<>();

    /**
     * 活动数据   weekId,groupId
     */
    public final static Map<Integer, Map<Integer, CrossKuangActivity>> activityMap = new ConcurrentHashMap<>();

    public static synchronized CrossKuangActivity getActivity(int groupId) {
        int weekId = GameConfig.getSystemThisWeekId();
        if (!matchGroupMap.containsKey(weekId) || !matchGroupMap.get(weekId).containsKey(groupId)) {
            // 没有分组数据
            return null;
        }
        if (!activityMap.containsKey(weekId)) {
            activityMap.put(weekId, new ConcurrentHashMap<>());
        }
        if (!activityMap.get(weekId).containsKey(groupId)) {
            CrossKuangActivity activity = new CrossKuangActivity(groupId);
            activityMap.get(weekId).put(groupId, activity);
        }
        return activityMap.get(weekId).get(groupId);
    }

    /**
     * 同步分组信息
     *
     * @param matchGroupList 匹配分组信息
     */
    public static void syncMatchGroup(List<KuangMatchGroup> matchGroupList) {
        int weekId = GameConfig.getSystemThisWeekId();
        if (!matchGroupMap.containsKey(weekId)) {
            matchGroupMap.put(weekId, new ConcurrentHashMap<>());
        }
        for (KuangMatchGroup matchGroup : matchGroupList) {
            matchGroupMap.get(weekId).put(matchGroup.getGroupId(), matchGroup);
        }
        // 加载下活动
        for (Integer groupId : matchGroupMap.get(weekId).keySet()) {
            getActivity(groupId);
        }
    }

    public static KuangMatchGroup getMatchGroup(int groupId) {
        return getMatchGroup(GameConfig.getSystemThisWeekId(), groupId);
    }

    public static KuangMatchGroup getMatchGroup(int weekId, int groupId) {
        if (!matchGroupMap.containsKey(weekId)) {
            return null;
        }
        return matchGroupMap.get(weekId).get(groupId);
    }

    /**
     * 内存移除过期的数据  保险一点，移除上上周,上一周的保留
     */
    @Override
    public boolean removeExpireData() throws Exception {
        int thisWeekId = GameConfig.getSystemThisWeekId();
        int preWeekId = GameConfig.getSystemPreWeekId();
        //  活动数据清一下
        for (Integer weekId : activityMap.keySet()) {
            if (weekId != thisWeekId && weekId != preWeekId) {
                activityMap.remove(weekId);
            }
        }
        // 分组信息也清一下
        for (Integer weekId : matchGroupMap.keySet()) {
            if (weekId != thisWeekId && weekId != preWeekId) {
                matchGroupMap.remove(weekId);
            }
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        addManagerReloadListener();
        return reload();
    }

    public void addManagerReloadListener() {
        ChannelSystemConfigMgr.addReloadListener(() -> {
            reloadKuangConfig();
            getLogger().info("KuangMgr addManagerReloadListener");
        });
        ConfigMgr.addReloadListener(() -> {
            reloadKuangConfig();
            getLogger().info("KuangMgr addManagerReloadListener");
        });
    }


    public static void reloadKuangConfig() {
        int thisWeekId = GameConfig.getSystemThisWeekId();
        getLogger().info("kuang reload config begin...");
        for (Map.Entry<Integer, Map<Integer, CrossKuangActivity>> activityEntry : activityMap.entrySet()) {
            if (activityEntry.getKey() != thisWeekId) {
                continue;
            }
            for (CrossKuangActivity activity : activityEntry.getValue().values()) {
                activity.reloadConfig();
            }
        }
        getLogger().info("kuang reload config end...");

    }

    @Override
    public boolean reloadConfig() throws Exception {
        reloadKuangConfig();
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        int thisWeekId = GameConfig.getSystemThisWeekId();
        List<Integer> groupList = KuangBusiness.getCrossKuangBattleGroupList(thisWeekId);
        for (Integer groupId : groupList) {
            getActivity(groupId);
        }
        return true;
    }




    @Override
    public boolean stop() {
        return true;
    }

    public static Map<Integer, Map<Integer, CrossKuangActivity>> getActivityMap() {
        return activityMap;
    }

    /**
     * 把上期的也加载下，防止奖励未发放
     * 进行中的活动
     */
    public static Map<Integer, CrossKuangActivity> getRunningActivityMap() {
        Map<Integer, CrossKuangActivity> map = new ConcurrentHashMap<>();
        int preWeekId = GameConfig.getSystemPreWeekId();
        if (activityMap.containsKey(preWeekId)) {
            map.putAll(activityMap.get(preWeekId));
        }
        int thisWeekId = GameConfig.getSystemThisWeekId();
        if (activityMap.containsKey(thisWeekId)) {
            map.putAll(activityMap.get(thisWeekId));
        }
        return map;
    }

    @Override
    public boolean save() {
        if (activityMap.size() == 0) {
            return true;
        }
        getLogger().info("kuang save start");
        for (Map<Integer, CrossKuangActivity> map : activityMap.values()) {
            for (CrossKuangActivity activity : map.values()) {
                activity.save();
            }
        }
        getLogger().info("kuang save end");
        return true;
    }

}
